package com.easygo.controller;

import com.easygo.client.ContentCategoryClient;
import com.easygo.common.PageUtils;
import com.easygo.common.ResultEnum;
import com.easygo.common.ResultResponse;
import com.easygo.pojo.ContentCategory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 徐策
 * @date 2020/1/8 17:19
 */
@Controller
@Scope("prototype")
public class ContentCategoryController {

    @Resource
    ContentCategoryClient contentCategoryClient;


    @RequestMapping("contentCategory_page/{indexPage}/{pageSize}")
    public String getContentByPage(@PathVariable("indexPage")Long indexPage
            , @PathVariable("pageSize")Long pageSize
            , @RequestParam(value = "name",defaultValue = "")String name, Model model){
        PageUtils<ContentCategory> pageUtils =  contentCategoryClient.getContentByPage(indexPage,pageSize,name);
        model.addAttribute("pageUtils",pageUtils);  //返回分页信息
        model.addAttribute("name",name);    //回显数据
        return "content_category";
    }

    //后台的新增广告分类controller
    @RequestMapping("contentCategory_add")
    @ResponseBody
    public ResultResponse<ContentCategory> contentCategory_add(ContentCategory contentCategory){
      return contentCategoryClient.contentCategory_add(contentCategory);
    }


    @ResponseBody
    @PostMapping("/contentCategory_deleteById/{id}")
    public ResultResponse delete(@PathVariable("id") Long id){
        Integer count = contentCategoryClient.isHasChild(id);
        if(count>0){
            return new ResultResponse(250,"该广告分类下有广告数据，暂时不能删除！");
        }else{
            return contentCategoryClient.contentCategory_deleteById(id);
        }
    }

    @ResponseBody
    @GetMapping("/contentCategory_id")
    public ContentCategory getcontentCategory_id(Long contentCategory_id){
        return contentCategoryClient.getById(contentCategory_id);
    }
}
